<template>
	<view class="wrap">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<input v-model="data.name" type="text" placeholder-class="line" placeholder="请填写收货人姓名" />
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<input v-model="data.phone" type="text" placeholder-class="line" placeholder="请填写收货人手机号" />
			</view>
			<view class="item" @tap="showRegionPicker">
				<view class="left">所在地区</view>
				<input v-model="data.address" type="text" placeholder-class="line" placeholder="详细地址,省市区县,乡镇" />
			</view>
		</view>
		<view class="addSite">
			<view class="add" @click="add">确认</view>
		</view>
	</view>
	<up-toast ref="uToastRef"></up-toast>
</template>

<script setup>
import { reactive, onMounted, ref } from 'vue';
import { AddCustomersAddress, GetCustomersAddress } from '../../apis/api.js';

const emits = defineEmits(['refresh']);

const data = reactive({
	name: '',
	address: '',
	phone: '',
	user_id: ''
});

const uToastRef = ref(null);
const toast = reactive({
	type: '',
	message: ''
});

// 获取user——id
onMounted(() => {
	uni.getStorage({
		key: 'userId',
		success: async (res) => {
			data.user_id = res.data;
			console.log('获取的 userId 为：', data.user_id);
		},
		fail: (error) => {
			console.error('获取 userId 失败：', error);
		}
	});
});

const add = () => {
	// console.log(data);
	// console.log(1);
	AddCustomersAddress({
		name: data.name,
		user_id: data.user_id,
		address: data.address,
		phone_number: data.phone
	}).then((res) => {
		if (res.msgType == 8000) {
			toast.type = 'success';
			toast.message = res.msg;
			uToastRef.value.show({ type: toast.type, message: toast.message, duration: 300 });
			// console.log(data.user_id);
			uni.redirectTo({
				url: '/pages/address/address'
			});
			GetCustomersAddress(data.user_id).then((res) => {
				if (res.msgType == 8000) {
					console.log(res);
					data.address = res.data;
				}
			});
		} else {
			toast.type = 'error';
			toast.message = res.msg;
			uToastRef.value.show({ type: toast.type, message: toast.message, duration: 300 });
		}
	});
};
</script>

<style lang="scss" scoped>
.wrap {
	background-color: #f2f2f2;
	.top {
		background-color: #ffffff;
		border-top: solid 2rpx $u-border-color;
		padding: 22rpx;
		.item {
			display: flex;
			font-size: 32rpx;
			line-height: 100rpx;
			align-items: center;
			border-bottom: solid 2rpx $u-border-color;
			.left {
				width: 180rpx;
			}
			input {
				text-align: left;
			}
		}
		.site-clipboard {
			padding-right: 40rpx;
			textarea {
				// width: 100%;
				height: 150rpx;
				background-color: #f7f7f7;
				line-height: 60rpx;
				margin: 40rpx auto;
				padding: 20rpx;
			}
			.clipboard {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 26rpx;
				color: $u-tips-color;
				height: 80rpx;
				.icon {
					margin-top: 6rpx;
					margin-left: 10rpx;
				}
			}
		}
	}
}

.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add {
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon {
			margin-right: 10rpx;
		}
	}
}
</style>
